class RegisterController{
    def init = {
        log.debug params
        RegisterCommand cmd = new RegisterCommand()
        cmd.city = session["mycity"]
        cmd.ownedBook = session["mybook"]
        cmd.wantedBook = params.id
        cmd.friendGender = session["targetGender"]? session["targetGender"] : 'F'
        switch(cmd.friendGender) {
            case 'M' : cmd.gender = 'F';break
            case 'F' : cmd.gender = 'M';break
            default : cmd.gender = 'M';
        }
        render(view:'register', model:[registerCommand:cmd])
    }

    def register = { RegisterCommand cmd ->
        if(cmd.hasErrors()) {
            render(view:'register',model:[registerCommand:cmd])
        }
        else {
            //render(view:'create',model:[userInstance:userInstance])
        }
    }
}

class RegisterCommand {
    String email
    String password
    String city
    String ownedBook
    String wantedBook
    String gender
    String friendGender

    static constraints = {
        city(blank:false)
        gender(blank:false, inList:['M','F'])
        friendGender(blank:false, inList:['M','F','N/A'])
        password(blank:false, minSize:6)

        email(blank:false,
            validator: {val ->
                if(!val.contains('@')) return false
                else return User.findByEmailIlike(val) == null
                })
    }
}
